package ocp

import (
    `net/url`
    `strconv`
    `errors`
    `net/http`
)

func GetUserHandlerV2(resp http.ResponseWriter, req *http.Request) {
    // validate inputs
    err := req.ParseForm()
    if err != nil {
        resp.WriteHeader(http.StatusInternalServerError)
        return
    }
    userID, err := extractUserID(req.Form)
    if err != nil {
        resp.WriteHeader(http.StatusPreconditionFailed)
        return
    }

    user := loadUser(userID)
    outputUser(resp, user)
}

func DeleUserHandlerV2(w http.ResponseWriter, r *http.Request) {
    // validate inputs
    err := r.ParseForm()
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        return
    }

    userID, err := extractUserID(r.Form)
    if err != nil {
        w.WriteHeader(http.StatusPreconditionFailed)
        return
    }

    deleteUser(userID)

}

func extractUserID(values url.Values) (int64, error) {
    userID, err := strconv.ParseInt(values.Get("userID"), 10, 64)
    if err != nil {
        return 0, err
    }

    if userID <= 0 {
        return 0, errors.New("userID must be positive")
    }
    return userID, nil

}
